<?php
namespace app\models;

/**
 * 管理员权限功能角色
 * Class Role
 * @package app\models
 */
class Role
{
    public static function getCount()
    {
        $cmd = \Yii::$app->db->createCommand('SELECT COUNT(1) FROM t_role');
        $count = $cmd->queryScalar();
        return $count;
    }

    /**
     * 获取角色列表
     * @param int $offset 分页查询起始位置
     * @param int $limit 分页查询每页数量
     * @return array
     */
    public static function getList($offset, $limit)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_role ORDER BY create_datetime DESC LIMIT :offset, :limit');
        $cmd->bindValue(':offset', $offset);
        $cmd->bindValue(':limit', $limit);

        $items = $cmd->queryAll();

        return $items;
    }

    /**
     * 获取所有启用的角色列表(不分页)
     * @return array
     */
    public static function getAllEnabledList()
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_role WHERE is_enabled=1 ORDER BY create_datetime DESC');
        $items = $cmd->queryAll();
        return $items;
    }

    public static function getDetail($roleId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_role WHERE id=:roleId');
        $cmd->bindValue(':roleId', $roleId);
        $item = $cmd->queryOne();
        if ($item == false) {
            return null;
        }

        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_role_function_point WHERE role_id=:roleId');
        $cmd->bindValue(':roleId', $roleId);
        $items = $cmd->queryAll();
        $item['role_function_point_list'] = $items;

        return $item;
    }

    /**
     * 添加、修改角色
     * @param $role
     * @return int
     * @throws \yii\db\Exception
     */
    public static function set($role)
    {
        $result = 0;
        $connection = \Yii::$app->db;
        $transaction = $connection->beginTransaction();
        try {
            $cmd = \Yii::$app->db->createCommand('call p_set_role(:in_role_id, :in_name, :in_description, :in_is_enabled, :in_is_system, :in_is_department)');
            $cmd->bindValue(':in_role_id', $role['role_id']);
            $cmd->bindValue(':in_name', $role['name']);
            $cmd->bindValue(':in_description', $role['description']);
            $cmd->bindValue(':in_is_enabled', $role['is_enabled']);
            $cmd->bindValue(':in_is_system', $role['is_system']);
            $cmd->bindValue(':in_is_department', $role['is_department']);
            $id = $cmd->queryScalar();
            $result += $id;

            $cmd = \Yii::$app->db->createCommand('call p_delete_role_function_point(:in_role_id)');
            $cmd->bindValue(':in_role_id', $role['role_id']);
            $cmd->execute();

            if (isset($role['function_point'])) {
                foreach ($role['function_point'] as $function_point) {
                    $cmd = \Yii::$app->db->createCommand('call p_add_role_function_point(:in_role_id, :in_function_point_id)');
                    $cmd->bindValue(':in_role_id', $id);
                    $cmd->bindValue(':in_function_point_id', $function_point);
                    $result += $cmd->execute();
                }
            }

            $transaction->commit();
        } catch (\Exception $e) {
            $result = 0;
            $transaction->rollBack();
        }

        return $result;
    }
}